package com.finalproject.ui;
/**
 * SnackItem representation for VendingMachine
 */
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.text.DecimalFormat;

import javax.swing.SwingConstants;

import com.finalproject.entity.SnackItem;
public class UIVBSnackItem extends JPanel {

	private JLabel nameLabel;
	private JLabel priceLabel;
	private JLabel caloriesLabel;
	private JButton buyButton;
	private JButton infoButton;
	private JLabel availableQuantity;
	private DecimalFormat priceFormat = new DecimalFormat("$0.00");
	
	private SnackItem snackItem;
	/**
	 * Create the panel.
	 */
	public UIVBSnackItem(SnackItem snackItem, Integer quantity) {
		setBorder(BorderFactory.createTitledBorder("Snack"));
		setPreferredSize(new Dimension(160,160));
		setSize(200,150);
		setVisible(true);
		if(snackItem == null) {
			add(new JLabel("SOLD OUT",JLabel.CENTER));
			return;
		}
		this.snackItem = snackItem;
		String name = snackItem.getName();
		Integer calories = snackItem.getCalories();
		String price = priceFormat.format(snackItem.getPrice());
		
		//setLayout(new FlowLayout());
		BorderLayout borderLayout = new BorderLayout();
		setLayout(borderLayout);
		borderLayout.setHgap(3);
		borderLayout.setVgap(3);
		
		
		
		JPanel labelPanel = new JPanel();
		BorderLayout labelBorderLayout = new BorderLayout();
		labelBorderLayout.setVgap(3);
		labelBorderLayout.setHgap(3);
		labelPanel.setLayout(labelBorderLayout);
		
		nameLabel = new JLabel(name,SwingConstants.CENTER);
		labelPanel.add(nameLabel,BorderLayout.NORTH);
		String calStr = "" + calories.toString()  +" cal";
		caloriesLabel = new JLabel(calStr,SwingConstants.CENTER);
		labelPanel.add(caloriesLabel,BorderLayout.CENTER);
		
		availableQuantity = new JLabel( "[" + quantity.toString()+"x Available]", SwingConstants.CENTER);
		
		priceLabel = new JLabel("(" + price.toString() +  ")",SwingConstants.CENTER);
		
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(priceLabel,BorderLayout.NORTH);
		
		centerPanel.add(availableQuantity,BorderLayout.CENTER);
		
		labelPanel.add(centerPanel,BorderLayout.SOUTH);
		
		
	
		add(labelPanel, BorderLayout.NORTH);
		nameLabel.setHorizontalTextPosition(JLabel.CENTER);

		
		infoButton = new JButton("Info");
		
		infoButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				showinfo();
			}
			
		});
		add(infoButton, BorderLayout.SOUTH);

		buyButton = new JButton("Buy");
		
		add(buyButton, BorderLayout.CENTER);
		
	//	setBorder(BorderFactory.createLineBorder(Color.black));
		
		
	}
	private void showinfo() {
		
		String name = "Name: " + snackItem.getName() + "\n";
	//	String description = "Description:" + snackItem.getDescription() + "\n";
		String description = "";
		String calories = "Calories: " + snackItem.getCalories().toString() + "cal\n";
		String price = "Price: " + priceFormat.format(snackItem.getPrice()) + "\n";
		String fat = "Fat: " + snackItem.getFat() +  "g\n";
		String cholestrol = "Cholestrol: " + snackItem.getCholestrol().toString() + "mg\n";
		String carbs = "Carbohydrate: " + snackItem.getCarbs().toString() + "g\n";
		String sodium = "Sodium: " + snackItem.getSodium().toString() + "mg\n";
		String protein = "Protein: " + snackItem.getProtein().toString() + "g";
		String message = name + description + calories + price + fat + cholestrol + carbs + sodium + protein ;
		JOptionPane.showConfirmDialog(this, message, "Snack Item information",JOptionPane.PLAIN_MESSAGE);

	}
	
	public JLabel getNameLabel() {
		return nameLabel;
	}
	
	public JButton getBuyButton() {
		return buyButton;
	}
	
	
	public JButton getInfoButton() {
		return infoButton;
	}
	
}
